###Figure A1###
ALLplotUK<- ggplot(merged_UK, aes(y = reorder(person, -estimate), x=estimate, color=villiannum)) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33", alpha=.5)+
  geom_vline(xintercept=.59, colour="#56B4E9", linetype = "solid", alpha=.5)+
  geom_vline(xintercept=.41, colour="#C5407EFF", linetype = "solid", alpha=.5)+
  geom_errorbar(aes(xmin=lower, xmax=upper), width=.07) +
  geom_text(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), nudge_x = -.125, size=2, show.legend = FALSE)+
  geom_text(aes(label=person), nudge_x = .15, size=2, show.legend = FALSE)+
  scale_color_manual(values = colors2)+
  scale_fill_manual(values = colors2)+
  geom_line() + 
  labs(x="Marginal mean", title="UK")+
  xlim(.1,.9)+
  geom_point()+
  theme_ggdist()+
  theme(axis.title.y = element_blank(),
        plot.title = element_text(face="bold"),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        legend.title = element_blank())+
  annotate("label", x = .65, y = 80, 
           label = "Mean projection\nof heroes: 58.6\n[57.8 , 59.4]", size=2, color="#56B4E9", fontface="bold")+
  annotate("label", x = .35, y = 10, 
           label = "Mean projection\nof villains: 41.4\n[40.6 , 42.3]", size=2, color="#C5407EFF", fontface="bold")

ALLplotUS<- ggplot(merged_US, aes(y = reorder(person, -estimate), x=estimate, color=villiannum)) + 
  geom_vline(xintercept = 0.50, linetype="dashed", colour="gray33", alpha=.5)+
  geom_vline(xintercept=.604, colour="#56B4E9", linetype = "solid", alpha=.5)+
  geom_vline(xintercept=.389, colour="#C5407EFF", linetype = "solid", alpha=.5)+
  geom_errorbar(aes(xmin=lower, xmax=upper), width=.07) +
  geom_text(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), nudge_x = -.125, size=2, show.legend = FALSE)+
  geom_text(aes(label=person), nudge_x = .15, size=2, show.legend = FALSE)+
  scale_color_manual(values = colors2)+
  scale_fill_manual(values = colors2)+
  geom_line() + 
  labs(x="Marginal mean", title="USA")+
  xlim(.1,.9)+
  geom_point()+
  theme_ggdist()+
  theme(axis.title.y = element_blank(),
        plot.title = element_text(face="bold"),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        legend.title = element_blank())+
  annotate("label", x = .65, y = 80, 
           label = "Mean projection\nof heroes: 60.4\n[59.5 , 61.3]", size=2, color="#56B4E9", fontface="bold")+
  annotate("label", x = .35, y = 10, 
           label = "Mean projection\nof villains 39.8\n[38.9 , 40.6]", size=2, color="#C5407EFF", fontface="bold")

plot <- ggarrange(ALLplotUS,ALLplotUK, ncol=2, nrow=1, common.legend=TRUE, legend="bottom",
                  align = "h") 
plot<- annotate_figure(plot,
                       top = text_grob("Pr(Projected partisanship matches respondent partisanship)",
                                       face = "bold", size=18, hjust=.5))
ggsave("figures_appendix/FigureA1.png",
       height=29, width=21, units="cm")
plot



###TABLE A5###
mm.1US  <- mm(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1US<-subset(mm.1US, feature %in% c("villian")) 

esttab <- select(MM.1US,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: shared party ID (US)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A5a.tex")


mm.1US  <- mm(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1US<-subset(mm.1US, feature %in% c("villian")) 

esttab <- select(MM.1US,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: ideological propinquity (US)", 
               digits = 3) -> esttab
xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A5b.tex")



###TABLE A6###
mm.1UK  <- mm(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1UK<-subset(mm.1UK, feature %in% c("villian")) 


esttab <- select(MM.1UK,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: shared party ID (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A6a.tex")


mm.1UK  <- mm(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1UK<-subset(mm.1UK, feature %in% c("villian")) 


esttab <- select(MM.1UK,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: ideological propinquity (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A6b.tex")



###TABLE A7###
stacked.strength0 <- cj(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                        estimate = "mm", by = ~ strengthOG)
stacked.strength0X<-subset(stacked.strength0, feature %in% c("villian")) 

esttab <- select(stacked.strength0X,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (shared party ID) by strength of ID (USA)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A7a.tex")

stacked.strength1 <- cj(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                        estimate = "mm", by = ~ strengthOG)
stacked.strength1X<-subset(stacked.strength1, feature %in% c("villian")) 

esttab <- select(stacked.strength1X,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (ideological propinquity) by strength of ID (USA)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A7b.tex")


###TABLE A8###
stacked.strength0K <- cj(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                         estimate = "mm", by = ~ strengthOG)
stacked.strength0XK<-subset(stacked.strength0K, feature %in% c("villian")) 

esttab <- select(stacked.strength0XK,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (shared party ID) by strength of ID (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A8a.tex")

stacked.strength1K <- cj(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                         estimate = "mm", by = ~ strengthOG)
stacked.strength1XK<-subset(stacked.strength1K, feature %in% c("villian")) 

esttab <- select(stacked.strength1XK,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (ideological propinquity) by strength of ID (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A8b.tex")

###TABLE A9###
mm.1US  <- mm(dfUS, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1US<-subset(mm.1US, feature %in% c("villiancat")) 

esttab <- select(MM.1US,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: shared party ID (US)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A9a.tex")


mm.1US  <- mm(dfUS, RILEdummymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1US<-subset(mm.1US, feature %in% c("villiancat")) 

esttab <- select(MM.1US,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: ideological propinquity (US)", 
               digits = 3) -> esttab
xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A9b.tex")



###TABLE A10###
mm.1UK  <- mm(dfUK, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1UK<-subset(mm.1UK, feature %in% c("villiancat")) 


esttab <- select(MM.1UK,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: shared party ID (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A10a.tex")


mm.1UK  <- mm(dfUK, RILEdummymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)
MM.1UK<-subset(mm.1UK, feature %in% c("villiancat")) 


esttab <- select(MM.1UK,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means: ideological propinquity (UK)", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_A10b.tex")

##FIGURE A2##
mm.1 <- mm(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)

plot(mm.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right") + 
  geom_label(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), x = 0.38, size=3)+
  theme(legend.position = "none") +
  geom_point(size=1.5)+
  labs(title="Pr(Projected partisanship matches respondent partisanship)")+
  theme(strip.background = element_rect(fill="#ffffff"),
        strip.text = element_text(face="bold", size=7),
        plot.title = element_text(face="bold"),
        axis.text.y = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, colour="gray33")

ggsave("figures_appendix/FigureA2.png",dpi=500, 
       height=18, width=18, units="cm")


###FIGURE A3###
mm.1 <- mm(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar, h0 = 0.5)

plot(mm.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right") + 
  geom_label(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), x = 0.4, size=3)+
  theme(legend.position = "none") +
  geom_point(size=1.5)+
  labs(title="Pr(Projected partisanship matches respondent partisanship)")+
  theme(strip.background = element_rect(fill="#ffffff"),
        strip.text = element_text(face="bold", size=7),
        plot.title = element_text(face="bold"),
        axis.text.y = element_text(face="bold")) + 
  geom_vline(xintercept = 0.50, colour="gray33")

ggsave("figures_appendix/FigureA3.png",dpi=500, 
       height=18, width=18, units="cm")


###FIGURE A4###
stacked.partyIDQ <- cj(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                       estimate = "mm", by = ~ IDasked)
stacked.partyIDQ<- subset(stacked.partyIDQ, feature %in% c("villian"))

plot(stacked.partyIDQ, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+ 
  labs(title="Pr(Projected partisanship matches respondent partisanship)",
       subtitle="Conditioned by named party in question (USA)",
       xlab="Marginal Mean")+
  theme(strip.background = element_rect(fill="#ffffff"),
        strip.text = element_text(face="bold", size=7),
        legend.title = element_blank(),
        plot.title = element_text(face="bold"),
        axis.text.y = element_text(face="bold"),
        axis.title.y = element_blank()) + 
  geom_vline(xintercept = 0.50, colour="gray33")

stacked.partyIDQ2 <- cj(dfUS, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                        estimate = "mm", by = ~ IDasked)
stacked.partyIDQ2<- subset(stacked.partyIDQ2, feature %in% c("villiancat"))

IDQplot1<- plot(stacked.partyIDQ2, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+ 
  xlab("Marginal Mean")+
  theme_ggdist()+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        legend.text = element_text(face="bold"),
        plot.subtitle = element_text(face="bold"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.title=element_blank(),
        axis.text.y = element_text(face="bold")) +
  geom_vline(xintercept = 0.50, colour="gray33")


mm.diff.IDQ2<- mm_diffs(dfUS, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, 
                        ~ IDasked, id = ~ IDvar)

mm.diff.IDQ2X<- subset(mm.diff.IDQ2, feature %in% c("villiancat"))

IDQplot2<- plot(mm.diff.IDQ2X, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+
  xlab("")+
  scale_color_manual(values = c("darkmagenta"))+
  geom_label(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), 
             nudge_x =.06, size=3, show.legend = FALSE)+
  xlim(-.2,.25)+
  xlab("Difference in MM")+
  theme_ggdist()+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        axis.ticks.y =element_blank(),
        legend.position ="none",
        axis.text.y =element_blank()) +
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")

combined1<- IDQplot1+IDQplot2+
  plot_annotation(title = "Pr(Projected partisanship matches respondent partisanship)",
                  subtitle="Conditioned by named party in question (US)",
                  theme = theme(plot.title = element_text(face = "bold"),
                                plot.subtitle = element_text(face = "bold")))
ggsave("figures_appendix/FigureA4a.png", dpi=500)
combined1

stacked.partyIDQ <- cj(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                       estimate = "mm", by = ~ IDasked)
stacked.partyIDQ<- subset(stacked.partyIDQ, feature %in% c("villian"))

plot(stacked.partyIDQ, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+ 
  labs(title="Pr(Projected partisanship matches respondent partisanship)",
       subtitle="Conditioned by named party in question (UK)",
       xlab="Marginal Mean")+
  theme(strip.background = element_rect(fill="#ffffff"),
        strip.text = element_text(face="bold", size=7),
        legend.title = element_blank(),
        plot.title = element_text(face="bold"),
        axis.text.y = element_text(face="bold"),
        axis.title.y = element_blank()) + 
  geom_vline(xintercept = 0.50, colour="gray33")

stacked.partyIDQ2 <- cj(dfUK, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                        estimate = "mm", by = ~ IDasked)
stacked.partyIDQ2<- subset(stacked.partyIDQ2, feature %in% c("villiancat"))

IDQplot1<- plot(stacked.partyIDQ2, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+ 
  xlab("Marginal Mean")+
  theme_ggdist()+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        legend.text = element_text(face="bold"),
        plot.subtitle = element_text(face="bold"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.title=element_blank(),
        axis.text.y = element_text(face="bold")) +
  geom_vline(xintercept = 0.50, colour="gray33")


mm.diff.IDQ2<- mm_diffs(dfUK, partymatch ~ villiancat + woman + house + british + imdb_quartiles + engagement_quartiles, 
                        ~ IDasked, id = ~ IDvar)

mm.diff.IDQ2X<- subset(mm.diff.IDQ2, feature %in% c("villiancat"))

IDQplot2<- plot(mm.diff.IDQ2X, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right")+
  xlab("")+
  scale_color_manual(values = c("darkmagenta"))+
  geom_label(aes(label=sprintf("%0.2f", round(estimate, digits = 2))), 
             nudge_x =.06, size=3, show.legend = FALSE)+
  xlim(-.2,.25)+
  xlab("Difference in MM")+
  theme_ggdist()+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        axis.ticks.y =element_blank(),
        legend.position ="none",
        axis.text.y =element_blank()) +
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")

combined1<- IDQplot1+IDQplot2+
  plot_annotation(title = "Pr(Projected partisanship matches respondent partisanship)",
                  subtitle="Conditioned by named party in question (UK)",
                  theme = theme(plot.title = element_text(face = "bold"),
                                plot.subtitle = element_text(face = "bold")))

ggsave("figures_appendix/FigureA4b.png", dpi=500)
combined1


###FIGURE C1###
dfUS <-dfUS%>% 
  mutate(knowtotal= as.factor(knowtotal))
dfUS$knowtotal <- factor(dfUS$knowtotal,
                         levels = c(0, .25, .5, .75, 1),
                         labels = c("0", "1", "2", "3", "4"))

stacked.know <- cj(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                   estimate = "mm", by = ~ knowtotal)

stacked.know<-subset(stacked.know, feature %in% c("villian")) 

plot(stacked.know, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right") + 
  theme_ggdist()+
  xlab("Marginal Mean")+
  labs(title="Shared partisan ID (US)",
       subtitle="Conditioned by # of correct franchise knowledge questions")+
  xlim(.25, .75)+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        legend.position ="bottom",
        legend.text = element_text(face="bold"),
        plot.title = element_text(face="bold"),
        plot.subtitle = element_text(face="bold"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.title=element_blank(),
        axis.text.y = element_text(face="bold")) +
  geom_vline(xintercept = .5, linetype="dashed", colour="gray33")

ggsave("figures_appendix/FigureC1a.png", dpi=500)

dfUK <-dfUK%>% 
  mutate(knowtotal= as.factor(knowtotal))
dfUK$knowtotal <- factor(dfUK$knowtotal,
                         levels = c(0, .25, .5, .75, 1),
                         labels = c("0", "1", "2", "3", "4"))

stacked.know <- cj(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                   estimate = "mm", by = ~ knowtotal)

stacked.know<-subset(stacked.know, feature %in% c("villian")) 

plot(stacked.know, feature_headers = FALSE) +
  ggplot2::aes(x = estimate, y = level, colour = BY) + 
  ggplot2::facet_wrap(~feature, ncol = 1L, labeller=labeller(feature = labels),
                      scales = "free_y", strip.position = "right") + 
  theme_ggdist()+
  xlab("Marginal Mean")+
  labs(title="Shared partisan ID (UK)",
       subtitle="Conditioned by # of correct franchise knowledge questions")+
  xlim(.25, .75)+
  theme(strip.text = element_blank(),
        axis.title.y =element_blank(),
        legend.position ="bottom",
        legend.text = element_text(face="bold"),
        plot.title = element_text(face="bold"),
        plot.subtitle = element_text(face="bold"),
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.title=element_blank(),
        axis.text.y = element_text(face="bold")) +
  geom_vline(xintercept = .5, linetype="dashed", colour="gray33")

ggsave("figures_appendix/FigureC1b.png", dpi=500)



###FIGURE C2###

dfUS <-dfUS%>% 
  mutate(knowtotalN= as.numeric(knowtotal))
dfUK <-dfUK%>% 
  mutate(knowtotalN= as.numeric(knowtotal))

modelknow<-lm (partymatch ~ villian*knowtotalN, data=dfUS)
modelknowUK<-lm (partymatch ~ villian*knowtotalN, data=dfUK)

predplot1 <-
  ggplot(dfUS, aes(knowtotalN, partymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none")+
  xlim(0, 4)+
  ylab("Pr(Projected partisanship)")+
  xlab("# of correct knowledge questions")+
  labs(subtitle="a) USA")+
  theme(plot.subtitle = element_text(face="bold"))+
  annotate(
    geom="label", x = 2.5, y =.505, size = 5, color="#56B4E9", fontface=2,
    label = "Slope for heroes")+
  annotate(
    geom="label", x = 2.5, y =.495, size = 5, color="#C5407EFF", fontface=2,
    label = "Slope for villains")

predplot2 <-
  ggplot(dfUK, aes(knowtotalN, partymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none")+
  xlim(0, 4)+
  ylab("")+
  xlab("# of correct knowledge questions")+
  labs(subtitle="b) UK")+
  theme(axis.text.y =element_blank(),
        axis.ticks.y =element_blank(),
        plot.subtitle = element_text(face="bold"))+
  annotate(
    geom="label", x = 2.5, y =.505, size = 5, color="#56B4E9", fontface=2,
    label = "Slope for heroes")+
  annotate(
    geom="label", x = 2.5, y =.495, size = 5, color="#C5407EFF", fontface=2,
    label = "Slope for villains")

plotC2<- predplot1+predplot2+
  plot_annotation(title = 'Study 1: Moderating effect of character knowledge',
                  theme = theme(plot.title = element_text(face = "bold", size=18)))
ggsave("figures_appendix/FigureC2.png", width = 24, height = 24, units = "cm")
plotC2

###TABLE C1###

stacked.party1 <- cj(dfUS, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                     estimate = "mm", by = ~ res_party2)
stacked.party1X<-subset(stacked.party1, feature %in% c("villian")) 

esttab <- select(stacked.party1X,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means of shared party ID by US party", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_C1a.tex")


stacked.party1 <- cj(dfUS, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                     estimate = "mm", by = ~ res_party2)
stacked.party1X<-subset(stacked.party1, feature %in% c("villian")) 

esttab <- select(stacked.party1X,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (ideological propinquity) by US party", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_C1b.tex")

###TABLE C2###

stacked.party1K <- cj(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                      estimate = "mm", by = ~ res_party2)
stacked.party1XK<-subset(stacked.party1K, feature %in% c("villian")) 

esttab <- select(stacked.party1XK,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means of shared party ID by UK party", 
               digits = 3) -> esttab
xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_C2a.tex")



stacked.party1K <- cj(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, id = ~ IDvar,
                      estimate = "mm", by = ~ res_party2)
stacked.party1XK<-subset(stacked.party1K, feature %in% c("villian")) 

esttab <- select(stacked.party1XK,
                 "Party" = BY,
                 "Feature" = feature,
                 "Level" = level,
                 "Est." = estimate,
                 "SE" = std.error,
                 "LowerCI" = lower,
                 "UpperCI" = upper)

export_mm <- bind_cols(esttab) %>%
  arrange(as.numeric(Feature), -as.numeric(Level)) %>%
  select(Party, Level, Est., SE, LowerCI, UpperCI)

xtable::xtable(export_mm, 
               caption = "Marginal means (ideological propinquity) by UK party", 
               digits = 3) -> esttab

xtable::print.xtable(esttab, 
                     include.rownames = F,
                     type = "latex",
                     file = "tables/Table_C2b.tex")


###FIGURE C3###

partydiff<- mm_diffs(dfUK, partymatch ~ person,h0 = 0.5, ~ res_party2, id = ~ IDvar)
partydiff_dist<- mm_diffs(dfUK, RILEdummymatch ~ person, h0 = 0.5, ~ res_party2, id = ~ IDvar)
partydiff <-partydiff%>% 
  mutate(person= as.factor(level))
partydiff_dist <-partydiff_dist%>% 
  mutate(person= as.factor(level))

party_merged = partydiff %>% 
  left_join(chardata, by = 'person')
party_merged <- subset(party_merged, villiannum!= "NA")

party_merged_dist = partydiff_dist %>% 
  left_join(chardata, by = 'person')
party_merged_dist <- subset(party_merged_dist, villiannum!= "NA")

party_merged <-party_merged%>% 
  mutate(level= as.factor(villiannum))
party_herosUK <- subset(party_merged, villiannum.mean==1)
party_villsUK <- subset(party_merged, villiannum.mean==2)

party_merged_dist <-party_merged_dist%>% 
  mutate(level= as.factor(villiannum))
party_herosUK_dist <- subset(party_merged_dist, villiannum.mean==1)
party_villsUK_dist <- subset(party_merged_dist, villiannum.mean==2)


mm.diff.party <- mm_diffs(dfUK, partymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                          ~ res_party2, id = ~ IDvar)
mm.diff.partyX<- subset(mm.diff.party, feature %in% c("villian"))

mm.diff.partyb <- mm_diffs(dfUK, RILEdummymatch ~ villian + woman + house + british + imdb_quartiles + engagement_quartiles, 
                           ~ res_party2, id = ~ IDvar)
mm.diff.partybX<- subset(mm.diff.partyb, feature %in% c("villian"))

mm.diff.partyX$lower95 <- mm.diff.partyX$estimate - (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$higher95 <- mm.diff.partyX$estimate + (1.96 * mm.diff.partyX$std.error)
mm.diff.partyX$lower99 <- mm.diff.partyX$estimate - (2.58 * mm.diff.partyX$std.error)
mm.diff.partyX$higher99 <- mm.diff.partyX$estimate + (2.58 * mm.diff.partyX$std.error)

mm.diff.partybX$lower95 <- mm.diff.partybX$estimate - (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$higher95 <- mm.diff.partybX$estimate + (1.96 * mm.diff.partybX$std.error)
mm.diff.partybX$lower99 <- mm.diff.partybX$estimate - (2.58 * mm.diff.partybX$std.error)
mm.diff.partybX$higher99 <- mm.diff.partybX$estimate + (2.58 * mm.diff.partybX$std.error)

party_main1<- mm.diff.partyX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.2, size = 4)+
  labs(y="Pairwise difference in MM\nDemocratic - Republican", x=NULL) +
  geom_jitter(data = party_herosUK, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = party_villsUK, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.5, .4)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="a) Left more likely to project than the right (partisan identities)")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")+
  annotate(
    geom="label", x = .3, y =1.5, size = 4, color = "#56b4e9", fontface=2,
    label = "Hero")+
  annotate(geom="label", x = .3, y =1.7, size = 4, color = "darkmagenta", fontface=2,
           label = "Villain")

party_main2<- mm.diff.partybX %>% 
  ggplot(aes(level, color=level, fill=level)) +
  geom_segment(aes(x=lower95, xend=higher95, y=level, yend=level), size=1, alpha=1)+ 
  geom_segment(aes(x=lower99, xend=higher99, y=level, yend=level), size=.25, alpha=.75)+
  geom_point(aes(x=estimate, y=level, fill=level), color="grey67", size=4,  pch=21)+
  geom_label(aes(x = estimate, y = level, label = sprintf("%0.2f", round(estimate, digits = 2))),
             fill = "white", nudge_y = 0.2, size = 4)+
  labs(y="", x=NULL) +
  geom_jitter(data = party_herosUK_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  geom_jitter(data = party_villsUK_dist, aes(x = estimate, y = level, color=level, fill=level), height=.1, size=2, alpha=.2)+
  xlim(-.5, .4)+
  theme_ggdist()+
  theme(legend.position = "none") +
  scale_color_manual(values = c("#56B4E9", "darkmagenta"))+
  scale_fill_manual(values = c("#56B4E9", "darkmagenta"))+
  labs(subtitle="b) Counter-projection larger than projection (ideological identities)")+
  theme(strip.background = element_blank(),
        strip.text = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        plot.subtitle = element_text(face="bold")) + 
  geom_vline(xintercept = 0, linetype="dashed", colour="gray33")


collapsed.dfUK<- summaryBy(outgrouplike + outgroupdislike + knowtotal + res_party2~ IDvar, data=dfUK)
collapsed.dfUK <- collapsed.dfUK %>% 
  mutate(res_party2 = as.factor(res_party2.mean),
         knowtotal = as.numeric(knowtotal.mean))

collapsed.dfUK <- subset(collapsed.dfUK, !is.na(res_party2))
collapsed.dfUK <- subset(collapsed.dfUK, !is.na(knowtotal))

modelX2 <-lm (outgroupdislike.mean ~ res_party2, collapsed.dfUK)
summary(modelX2)
collapsed.dfUK$predicted<-predict(modelX2, collapsed.dfUK)

knowmeanUK <- ddply(collapsed.dfUK, "res_party2", summarise, meanx2=mean(knowtotal))
collapsed.dfUK <- collapsed.dfUK %>% 
  mutate(outgroupdislike.mean = outgroupdislike.mean/10)

partymeanUK <- ddply(collapsed.dfUK, "res_party2", summarise, meanx=mean(outgroupdislike.mean))


UKhist<- ggplot(collapsed.dfUK, aes(x=outgroupdislike.mean, fill=res_party2)) +
  geom_density(alpha=.8) +
  theme_ggdist()+
  xlim(0,1)+
  scale_fill_manual(values = c("blue2", "red2"), labels=c("Labour", "Conservatives")) +
  scale_color_manual(values = c("blue2", "red2")) +
  geom_vline(data=partymeanUK, aes(xintercept=meanx,  colour=res_party2),
             linetype="dashed", size=1, alpha=.5)+
  labs(y="Density", x="(Self-reported) negative out-group affect", subtitle="c) Left hold more negative affects towards out-group than vice versa ")+
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"))+
  annotate("label", x = 0, y = 2.15, label = "Conservatives",
           color="blue2", size=4, fontface="bold", hjust=0)+
  annotate("label", x = 0, y = 2.4, label = "Labour",
           color="red2", size=4, fontface="bold", hjust=0)
modelparty<-lm (partymatch ~ villian*outgroupdislike, data=dfUK)
dfUK$predictINT1<-predict(modelparty, dfUK)

modelID<-lm (RILEdummymatch ~ villian*outgroupdislike, data=dfUK)
dfUK$predictINT2<-predict(modelID, dfUK)

treat <- subset(dfUK, villian== "Hero")
control <- subset(dfUK, villian== "Villain")

gg_df1 <-
  modelparty %>%
  margins(at = list(outgroupdislike = seq(0, 10, by = 1))) %>%
  summary %>%
  as.data.frame() %>%
  filter(factor == "villianVillain")

gg_df2 <-
  modelID %>%
  margins(at = list(outgroupdislike = seq(0, 10, by = 1))) %>%
  summary %>%
  as.data.frame() %>%
  filter(factor == "villianVillain")



predplot1 <-
  ggplot(dfUK, aes(outgroupdislike, partymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none")+
  xlim(0, 10)+
  ylab("Pr(Projected partisanship)")+
  xlab("(Self-reported) negative out-group affect")+
  labs(subtitle="d) Negative out-group affect determines Pr(Shared partisanship)")+
  theme(axis.text.x =element_blank(),
        axis.ticks.x =element_blank(),
        plot.subtitle = element_text(face="bold"))+
  annotate(
    geom="label", x = 7.5, y =.525, size = 5, color="#56B4E9", fontface=2,
    label = "Slope for heroes")+
  annotate(
    geom="label", x = 7.5, y =.475, size = 5, color="#C5407EFF", fontface=2,
    label = "Slope for villains")


predplot2 <-
  ggplot(dfUK, aes(outgroupdislike, RILEdummymatch, group = villian, color=villian)) +
  scale_color_manual(values = c("#56B4E9", "#C5407EFF")) +
  scale_fill_manual(values = c("#56B4E9", "#C5407EFF")) +
  theme_ggdist()+
  geom_hline(yintercept = 0.50, linetype="dashed", colour="gray33")+
  stat_smooth(method = "lm_robust", fullrange = TRUE, se=TRUE) +
  theme(legend.position = "none",
        plot.subtitle = element_text(face="bold"))+
  xlim(0, 10)+
  ylab("Pr(Projeced ideology)")+
  xlab("(Self-reported) negative out-group affect")+
  labs(subtitle="e) Negative out-group affect determines Pr(Shared ideology)")

study1_party<- (party_main1+party_main2) / UKhist /(predplot1+predplot2)
FigC3<- study1_party+
  plot_annotation(title = 'Study 1: Party & affect-based exploratory analyses (British data)',
                  theme = theme(plot.title = element_text(face = "bold", size=18)))
ggsave("figures_appendix/FigureC3.png", width = 32, height = 32, units = "cm")
FigC3





###SUMMARY STATS: TABLES A1-A4###
dfsumUK <- read_dta("data/study1_UK_respondents.dta")
dfsumUS <- read_dta("data/study1_US_respondents.dta")

dfsumUK <-dfsumUK%>% 
  mutate(
    gender=as.factor(gender),
    sexorient=as.factor(sexorient),
    degree=as.factor(degree),
    ethnic2=as.factor(ethnic2),
    partyIDcat=as.factor(partyIDcat),
    IDstrength=as.factor(IDstrength),
    voteleave=as.factor(voteleave))

dfsumUS <-dfsumUS%>% 
  mutate(
    gender=as.factor(gender),
    sexorient=as.factor(sexorient),
    degree=as.factor(degree),
    ethnic=as.factor(ethnic),
    partyID=as.factor(partyID),
    IDstrength=as.factor(IDstrength))

datasummary_skim(dfsumUK, type="categorical",  output='tables/Table_A1.tex')
datasummary_skim(dfsumUK, output='tables/Table_A2.tex')

datasummary_skim(dfsumUS, type="categorical", output='tables/Table_A3.tex')
datasummary_skim(dfsumUS, output='tables/Table_A4.tex')